﻿#include "view.h"
#include "ui_view.h"

View::View(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::View)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose);     //设置关闭该窗口时，进行析构
    setWindowModality(Qt::ApplicationModal);    //设置窗口模态
    
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("./info.db");
    
    if(!db.open())
    {
        qDebug()<<"数据库打开失败!"<<db.lastError().text();
        //QMessageBox::warning(this,"错误!",db.lastError().text());
        return;
    }
    
    QSqlTableModel *model = new QSqlTableModel(ui->tableView);
    model->setTable("info");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->select(); //选区整个表的所有行
    
    ui->tableView->setSelectionBehavior(QTableView::SelectRows);    //选中整行
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);    //只能单选
    ui->tableView->setEditTriggers(QTableView::NoEditTriggers); //禁止编辑
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);     //调整列宽以适应单元内容
    ui->tableView->setModel(model);

    connect(ui->tableView,&QTableView::clicked,[=](const QModelIndex &index){
        QModelIndex i = model->index(index.row(),3);
        QModelIndex j = model->index(index.row(),4);
        QModelIndex k = model->index(index.row(),5);
        
        emit selectRow(model->data(i).toInt(), model->data(j).toInt(), model->data(k).toInt());
    });
}

View::~View()
{
    delete ui;
}

void View::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    //设置背景图片
    painter.drawPixmap(0,0,width(),height(),QPixmap("://1.jpg"));
}
